/**
 * 登录模块 使用 db 文件来保存账号信息
 * 查看 db 文件请用 sql 可视化工具
 */
import * as Mock from 'mockjs'
import { md5s, Exception, Succeed } from './index'
import Jwt from '../utils/token'

import { User } from '../entity/User'

import { getRepository, getConnection } from 'typeorm'

const niname = Mock.mock('@cname')
const ager = Mock.mock('@integer(18,30)')
const gender = Mock.mock('@integer(0,1)')

/** 登录函数 */
export const post_login = async (req: any, res: any) => {
	let tip: any
	try {
		const { username, password } = req.body
		const user = await is_username(username)
		if (user && user.password == password) {
			const name = md5s(username).substring(10)
			const token = new Jwt(name).set_token()
			const id = user.id
			tip = Succeed({ token, id }, '登录成功')
		} else {
			tip = new Exception(400, '账号或密码错误').toObject()
		}
	} catch (error) {
		tip = new Exception(400, error).toObject()
	}
	res.json(tip)
}
/** 登录函数 */
export const post = (req: any, res: any) => {
	const { username, password } = req.body
	async function sql() {
		// await getConnection()
		// 	.createQueryBuilder()
		// 	.insert()
		// 	.into(User)
		// 	.values([
		// 		{ username: '100861', password: '123' },
		// 		{ username: '100862', password: '123' },
		// 		{ username: '100863', password: '123' },
		// 		{ username: '100864', password: '123' },
		// 		{ username: '100865', password: '123' },
		// 		{ username: '100866', password: '123' },
		// 		{ username: '100867', password: '123' },
		// 		{ username: '100868', password: '123' },
		// 		{ username: '100869', password: '123' }
		// 	])
		// 	.execute()
		const userRepository = getRepository(User)
		const user = await userRepository.find()
		console.log('user', user)
		return user
	}
	sql()
	res.json(req.body)
}

/** 注册函数 */
export const post_register = async (req: any, res: any) => {
	let tip: any = new Exception(500, '注册失败').toObject()
	try {
		const { username } = req.body
		const user = await is_username(username)
		if (user) {
			tip = new Exception(400, '注册失败 , 账号已被注册').toObject()
		} else {
			const users = await set_username(req.body)
			if (users) {
				tip = Succeed({}, '注册成功')
			}
		}
	} catch (error) {
		tip = new Exception(404, error).toObject()
	}
	res.json(tip)
}

/**
 * 检测账号是否已经存在
 * @param username 需要检测的账号
 * @return 返回账号信息 , 无就返回 false
 */
const is_username = async (username: string) => {
	try {
		const userRepository = getRepository(User)
		const user = await userRepository.findOne({ username })
		if (user) {
			return user
		}
		return false
	} catch (error) {
		return false
	}
}

/**
 * 存入注册账号信息
 * @param data 注册信息
 * @return 返回存入结果
 */
const set_username = async (data: any) => {
	const { username, password, nickname = Mock.mock('@name') } = data
	try {
		await getConnection()
			.createQueryBuilder()
			.insert()
			.into(User)
			.values([
				{
					username,
					password,
					nickname,
					gender: Mock.mock('@integer(0,1)'),
					age: Mock.mock('@integer(18,30)'),
					img: `https://img.yzcdn.cn/vant/apple-${Mock.mock(
						'@integer(1,8)'
					)}.jpg`
				}
			])
			.execute()
		return true
	} catch (error) {
		return false
	}
}
